<?php
//Cleans user input
function cleanInput($input) {
 
  $search = array(
    '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
  );
 
    $output = preg_replace($search, '', $input);
    return $output;
  }
  
  //
function is_ip_valid($ip) {

    if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) {
        return true;
    }

    return false;
}

//
function is_valid_email($email, $test_mx = false) {
    if (eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
        if ($test_mx) {
            list($username, $domain) = split("@", $email);
            return getmxrr($domain, $mxrecords);
        }
        else
            return true;
    else
        return false;
}

//Validate a specified timestamp
function is_valid_timestamp($timestamp) {
// ------------------------------------- 
// $timestamp - format of yyyy-mm-dd hh:mm:ss 
// ------------------------------------- 

    if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $timestamp, $matches)) {
        if (checkdate($matches[2], $matches[3], $matches[1])) {
            return true;
        }
    }
    return false;
}

//Validate credit card number
function validate_creditcard($card_number) {
// -------------------------------------
//   Check if a card number follows the Luhn algorithm
// -------------------------------------
// Remove the dashes
    $card_number = str_replace('-', '', $card_number);

// Convert to an array of digits
    $arrCardDigits = array();
    for ($i = 0; $i < strlen($card_number); $i++) {
        $arrCardDigits[$i] = (int) $card_number[$i];
    }

// Start the algorithm
    $sum = 0;
    $alt = false;
    for ($i = count($arrCardDigits) - 1; $i >= 0; $i--) {
        if ($alt) {
            $arrCardDigits[$i] *= 2;
            if ($arrCardDigits[$i] > 9)
                $arrCardDigits[$i] -= 9;
        }
        $sum += $arrCardDigits[$i];
        $alt = !$alt;
    }
    return $sum % 10 == 0;
}


?>